/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: likangselect.c
> Author: lv1
> Website:www.lv1.org
> Created Time: 2024年12月04日 星期三 17时24分07秒
***************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "cJOSN.c"

int main(int argc,const char* argv[])
{
	//创建套接字
	int server_socket = socket(AF_INET,SOCK_STREAM,0);

	if(argc != 3)
	{
		printf("cmd ip port\n");	
		return -1;
	}
	
	struct sockaddr_in saddr;
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(atoi(argv[2]));    
	saddr.sin_addr.s_addr = inet_addr(argv[1]);   

	int optval = 1;
	setsockopt(server_socket,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval));
	//绑定
	int isbind = bind(server_socket,(struct sockaddr*)&saddr,sizeof(saddr));
	if(isbind == -1)
	{
		perror("bind");
		return -1;
	}
	//启动监听
	int islisten = listen(server_socket,10);
	if(islisten == -1)
	{
		perror("listen");
		return -1;
	}
	printf("监听已启动\n");
	
	struct sockaddr_in caddr;
	int addrlen = sizeof(caddr);
	int client_socket = accept(server_socket,(struct sockaddr*)&caddr,&addrlen);
	printf("连接已经建立\n");

	
	
	close(client_socket);
    return 0;
}

char *packJson()
{

	cJSON* root = cJSON_CreateObject();
	
	printf("input nursename: \n");
	scanf("%s",nursename);
	cJSON* nursername = cJSON_CreateString("nursename");
	printf("input passwd: \n");
	scanf("%s",passwd);
	cJSON* passwd = cJSON_CreateString("passwd");
	
	cJSON_AddItemToObject(root,"nursename",nursename);
	cJSON_AddItemToObject(root,"passwd",passwd);

	char *p = (char *)malloc(1024);
	p = cJSON_Print(root);

	printf("%s\n",p);
	cJSON_Delete(root);
	return p;
}
